package qhdx.fiveGC.config;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName DataSourceConfig
 * @Description TODO
 * @Author wno704
 * @Date 2022/3/18 11:28
 * @Version 1.0
 */
@Configuration
public class DataSourceConfig {
    //服保生产环境数据库
    @Bean(name = "dataSourceFbProd")
    @ConfigurationProperties("spring.datasource.druid.fbprod")
    public DataSource dataSourceFbProd(){
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "jdbcTemplateFbProd")
    public JdbcTemplate jdbcTemplateFbProd(
            @Qualifier("dataSourceFbProd") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

//    服保报表环境数据库
    @Bean(name = "dataSourceFbReport")
    @ConfigurationProperties("spring.datasource.druid.fbreport")
    public DataSource dataSourceFbReport(){
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "jdbcTemplateFbReport")
    public JdbcTemplate jdbcTemplateFbReport(
            @Qualifier("dataSourceFbReport") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    //调度生产环境数据库
    @Bean(name = "dataSourceDdProd")
    @ConfigurationProperties("spring.datasource.druid.ddprod")
    public DataSource dataSourceDdProd(){
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "jdbcTemplateDdProd")
    public JdbcTemplate jdbcTemplateDdProd(
            @Qualifier("dataSourceDdProd") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    //调度报表环境数据库
//    @Bean(name = "dataSourceDdReport")
//    @ConfigurationProperties("spring.datasource.druid.ddreport")
//    public DataSource dataSourceDdReport(){
//        return DruidDataSourceBuilder.create().build();
//    }
//
//    @Bean(name = "jdbcTemplateDdReport")
//    public JdbcTemplate jdbcTemplateDdReport(
//            @Qualifier("dataSourceDdReport") DataSource dataSource) {
//        return new JdbcTemplate(dataSource);
//    }

    @Bean(name = "dataSourceDb166pg")
    @ConfigurationProperties(prefix = "spring.datasource.druid.db166pg")
    public DataSource dataSourceDb166pg(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "jdbcTemplateDb166pg")
    public JdbcTemplate jdbcTemplateDb166pg(
            @Qualifier("dataSourceDb166pg") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

//    @Bean
//    public SqlSessionFactory sqlSessionFactory() throws Exception {
//        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
//        sessionFactoryBean.setDataSource(defaultDataSource()); // 设置默认数据源
//        // 配置其他 MyBatis 相关属性...
//        sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
//                .getResources("classpath:mapper/*.xml"));
//        return sessionFactoryBean.getObject();
//    }

//    @Bean
//    public DataSourceTransactionManager transactionManager() {
//        return new DataSourceTransactionManager(defaultDataSource());
//    }
    @Primary
    @Bean(name = "dynamicDS1")
    public DataSource dataSource(){
        //配置默认数据源
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        dynamicDataSource.setDefaultTargetDataSource(dataSourceDdProd());
        //配置多数据源
        Map<Object, Object> dsMap = new HashMap<>();
        dsMap.put("db1",dataSourceDdProd());
//        dsMap.put("db2",dataSourceFbReport());
//        dsMap.put("db3",dataSourceDdReport());
//        dsMap.put("db4",dataSourceFbProd());
        dsMap.put("db5",dataSourceDb166pg());
        dynamicDataSource.setTargetDataSources(dsMap);
        return dynamicDataSource;
    }
    private DataSource defaultDataSource() {
        return dataSourceDdProd(); // 返回默认的数据源（dataSource1）
    }
    @Bean
    public PlatformTransactionManager transactionManager(){
        return new DataSourceTransactionManager(dataSource());
    }



}
